package com.baomidou.springwind.controller.supply;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.service.NetworkSupplyService;
import com.baomidou.springwind.service.ProjectService;
import com.baomidou.springwind.utils.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.springwind.mapper.NetworkSupplyDao;
import java.util.*;

/**
 * <p>
 * 物流供应链
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/supply")
public class CustomController extends BaseController {


    @Autowired
    private NetworkSupplyService networkSupplyService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private NetworkSupplyDao networkSupplyDao;
    /**
     * 客户分析主界面
     */
    @RequestMapping("/custom")
    @Permission("1001")
    public String customGroup(Model model) {
        EntityWrapper<NetworkSupply> wrapper = new EntityWrapper<NetworkSupply>();
        wrapper.setSqlSelect("delivery_center deliveryCenter,delivery_id deliveryId");
        wrapper.eq("batch_id",getProjectId());
        wrapper.groupBy("delivery_id");
        model.addAttribute("prjList", networkSupplyService.selectList(wrapper));
        return pjaxPath("/lpp/network/supply/custom/main", model);
    }


    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/bar1")
    public String getBar1(Long batchId, Model model) {
        String delivery=request.getParameter("center");
        if (delivery.length()<0||delivery==""){
            delivery=getDelivery();
        }
        EntityWrapper<NetworkSupply> wrapper = new EntityWrapper<NetworkSupply>();
        wrapper.eq("delivery_id",delivery);
        wrapper.groupBy("sale_dafang");
        wrapper.setSqlSelect("sum(order_quantity) quantity,sale_dafang sale");
        wrapper.eq("batch_id", getProjectId());
        List<Map<String, Object>> mapList = networkSupplyService.selectMaps(wrapper);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray dataAxis=new JSONArray();
        JSONArray data=new JSONArray();
        JSONArray data2=new JSONArray();
        JSONObject a1 = null;
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            a1 = new JSONObject();
            a1.put("name",mapList.get(i).get("sale").toString().substring(0,6));
            a1.put("value",mapList.get(i).get("quantity"));
            data2.add(mapList.get(i).get("quantity"));
            dataAxis.add(mapList.get(i).get("sale").toString().substring(0,6));
            data.add(a1);
        }
        ret.put("x",data);
        ret.put("x2",data2);
        ret.put("x1",dataAxis);

        return callbackSuccess(ret);
    }
    public String getDelivery(){
        ParamMap<String,Object> pm=new ParamMap<String, Object>();
        pm.put("batchId",getProjectId());
        pm.put("operatorId",getCurrentUserId());
        List<Map<String, Object>> mapList = networkSupplyDao.selectDelivery(pm);
        if(mapList!=null){
            return mapList.get(0).get("deliveryId").toString();
        }
        return null;
    }
    public String getPrimary(String delivery){
        ParamMap<String,Object> pm=new ParamMap<String, Object>();
        pm.put("batchId",getProjectId());
        pm.put("operatorId",getCurrentUserId());
        pm.put("deliveryId",delivery);
        List<Map<String, Object>> mapList = networkSupplyDao.selectPrimary(pm);
        if(mapList!=null){
            return mapList.get(0).get("primarySales").toString();
        }
        return null;
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/option2")
    public String getOption1(Long batchId, Model model) {
        JSONObject ret = new JSONObject();
        String delivery=request.getParameter("center");
        EntityWrapper<NetworkSupply> wrapper1 = new EntityWrapper<NetworkSupply>();
        wrapper1.setSqlSelect("primary_sales primarySales");
        wrapper1.eq("batch_id",getProjectId());
        wrapper1.eq("delivery_id",delivery);
        wrapper1.groupBy("primary_sales");
        List<NetworkSupply> list1=networkSupplyService.selectList(wrapper1);
        ret.put("option2",list1);
        return callbackSuccess(ret);
    }
    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/option4")
    public String getOption4() {
        JSONObject ret = new JSONObject();
        String delivery=request.getParameter("center");
        EntityWrapper<NetworkSupply> wrapper2 = new EntityWrapper<NetworkSupply>();
        wrapper2.setSqlSelect("sale_dafang saleDafang");
        wrapper2.eq("batch_id",getProjectId());
        wrapper2.groupBy("sale_dafang");
        wrapper2.eq("delivery_id",delivery);
        List<NetworkSupply> list1=networkSupplyService.selectList(wrapper2);
        ret.put("option4",list1);
        return callbackSuccess(ret);
    }
    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/option6")
    public String getOption6() {
        JSONObject ret = new JSONObject();
        String delivery=request.getParameter("center");
        EntityWrapper<NetworkSupply> wrapper2 = new EntityWrapper<NetworkSupply>();
        wrapper2.setSqlSelect("order_type orderType");
        wrapper2.eq("delivery_id",delivery);
        wrapper2.eq("batch_id",getProjectId());
        wrapper2.groupBy("order_type");
        List<NetworkSupply> list1=networkSupplyService.selectList(wrapper2);
        ret.put("option6",list1);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/bar2")
    public String getBar2() {
       String center=request.getParameter("center");
       String primarySales=request.getParameter("primarySales");
        if (center.length()<0||center==""){
            center=getDelivery();
        }
        if (primarySales.length()<0||primarySales==""){
            primarySales=getPrimary(center);
        }
        EntityWrapper<NetworkSupply> wrapper = new EntityWrapper<NetworkSupply>();
        wrapper.setSqlSelect("sum(order_quantity)  quantity,sale_dafang sale,count(order_quantity) counts");
        wrapper.eq("delivery_id",center);
        wrapper.eq("primary_sales",primarySales);
        wrapper.eq("batch_id",getProjectId());
        wrapper.groupBy("sale_dafang");
        List<Map<String,Object>> mapList = networkSupplyService.selectMaps(wrapper);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray data=new JSONArray();
        JSONArray y1=new JSONArray();
        JSONArray y2=new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            data.add(mapList.get(i).get("sale"));
            y1.add(mapList.get(i).get("quantity"));
            y2.add(mapList.get(i).get("counts"));
        }
        ret.put("data1",data);
        ret.put("data2",y1);
        ret.put("data3",y2);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/bar3")
    public String getBar3(Long batchId, Model model) {
        String center=request.getParameter("center");
        if (center.length()<0||center==""){
            center=getDelivery();
        }
        EntityWrapper<NetworkSupply> wrapper = new EntityWrapper<NetworkSupply>();
        wrapper.setSqlSelect("sum(order_quantity)  quantity,order_type types");
        wrapper.eq("delivery_id",center);
        wrapper.eq("batch_id",getProjectId());
        wrapper.groupBy("order_type");
        List<Map<String,Object>> mapList = networkSupplyService.selectMaps(wrapper);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        EntityWrapper<NetworkSupply> wrapper1 = new EntityWrapper<NetworkSupply>();
        wrapper1.setSqlSelect("sum(order_quantity)  quantity,sale_dafang sale");
        wrapper1.eq("delivery_id",center);
        wrapper1.eq("batch_id",getProjectId());
        wrapper1.groupBy("order_type");
        wrapper1.groupBy("sale_dafang");
        List<Map<String,Object>> mapList1 = networkSupplyService.selectMaps(wrapper1);
        JSONArray y1=new JSONArray();
        JSONArray y2=new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            JSONObject data1=new JSONObject();
            data1.put("name",mapList.get(i).get("types"));
            data1.put("value",mapList.get(i).get("quantity"));
            y1.add(data1);
        }
        for (int i=0;i<mapList1.size();i++){
            JSONObject data2=new JSONObject();
            data2.put("name",mapList1.get(i).get("sale"));
            data2.put("value",mapList1.get(i).get("quantity"));
            y2.add(data2);
        }
        ret.put("data1",y1);
        ret.put("data2",y2);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/bar4")
    public String getBar4(Long batchId, Model model) {
        Map<String, Object> pm = getParameterMap();
        pm.put("batchId", getProjectId());
        String center=null;
        if (pm.get("center").toString().length()<0||pm.get("center").toString()==""){
            center=getDelivery();
            pm.put("center",center);
        }
        if (pm.get("sales").toString().length()<0||pm.get("sales").toString()==""){
            pm.put("sales",getSales(center));
        }
        List<Map<String,Object>> mapList = networkSupplyDao.selectSalesQuantityMonths(pm);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray y1=new JSONArray();
        JSONArray y2=new JSONArray();
        JSONArray y3=new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
           y1.add(mapList.get(i).get("months"));
           y2.add(mapList.get(i).get("quantity"));
           y3.add(mapList.get(i).get("counts"));
        }
        ret.put("data1",y1);
        ret.put("data2",y2);
        ret.put("data3",y3);
        return callbackSuccess(ret);
    }

    public String getSales(String delivery){
        ParamMap<String,Object> pm=new ParamMap<String, Object>();
        pm.put("batchId",getProjectId());
        pm.put("operatorId",getCurrentUserId());
        pm.put("deliveryId",delivery);
        List<Map<String, Object>> mapList = networkSupplyDao.selectSales(pm);
        if(mapList!=null){
            return mapList.get(0).get("saleDafang").toString();
        }
        return null;
    }
    public String getType(String delivery){
        ParamMap<String,Object> pm=new ParamMap<String, Object>();
        pm.put("batchId",getProjectId());
        pm.put("operatorId",getCurrentUserId());
        pm.put("deliveryId",delivery);
        List<Map<String, Object>> mapList = networkSupplyDao.selectType(pm);
        if(mapList!=null){
            return mapList.get(0).get("orderType").toString();
        }
        return null;
    }
    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/bar5")
    public String getBar5(Long batchId, Model model) {
        Map<String, Object> pm = getParameterMap();
        pm.put("batchId", getProjectId());
        String center=null;
        if (pm.get("center").toString().length()<0||pm.get("center").toString()==""){
            center=getDelivery();
            pm.put("center",center);
        }
        if (pm.get("primarySales").toString().length()<0||pm.get("primarySales").toString()==""){
            pm.put("primarySales",getPrimary(center));
        }
        List<Map<String,Object>> mapList = networkSupplyDao.selectcenterMonthsQuantity(pm);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray data=new JSONArray();
        JSONArray y1=new JSONArray();
        JSONArray y2=new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            data.add(mapList.get(i).get("months"));
            y1.add(mapList.get(i).get("quantity"));
            y2.add(mapList.get(i).get("counts"));
        }
        ret.put("data1",data);
        ret.put("data2",y1);
        ret.put("data3",y2);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/bar51")
    public String getBar51(Long batchId, Model model) {
        Map<String, Object> pm = getParameterMap();
        pm.put("batchId", getProjectId());
        if (pm.get("center").toString().length()<0||pm.get("center").toString()==""){
            pm.put("center",getDelivery());
        }
        if (pm.get("primarySales").toString().length()<0||pm.get("primarySales").toString()==""){
            pm.put("primarySales","日日顺店");
        }
        List<Map<String,Object>> mapList = networkSupplyDao.selectcenterMonthsQuantity2(pm);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray data=new JSONArray();
        JSONArray y1=new JSONArray();
        JSONArray y2=new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            data.add(mapList.get(i).get("sale_dafang"));
            y1.add(mapList.get(i).get("quantity"));
            y2.add(mapList.get(i).get("counts"));
        }
        ret.put("data1",data);
        ret.put("data2",y1);
        ret.put("data3",y2);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/bar6")
    public String getBar6(Long batchId, Model model) {
        Map<String, Object> pm = getParameterMap();
        pm.put("batchId", getProjectId());
        String center=null;
        if (pm.get("center").toString().length()<0||pm.get("center").toString()==""){
            center=getDelivery();
            pm.put("center",center);
        }
        if (pm.get("orderType").toString().length()<0||pm.get("orderType").toString()==""){
            pm.put("orderType",getType(center));
        }
        List<Map<String,Object>> mapList = networkSupplyDao.select1(pm);
        if(mapList.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray data=new JSONArray();
        JSONArray y1=new JSONArray();
        JSONArray y2=new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            data.add(mapList.get(i).get("sale"));
            y1.add(mapList.get(i).get("quantity"));
            y2.add(mapList.get(i).get("counts"));
        }
        ret.put("data1",data);
        ret.put("data2",y1);
        ret.put("data3",y2);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/custom/bar61")
    public String getBar61(Long batchId, Model model) {
        Map<String, Object> pm = getParameterMap();
        pm.put("batchId",getProjectId());
        if (pm.get("center").toString().length()<0||pm.get("center").toString()==""){
            pm.put("center",getDelivery());
        }
        if (pm.get("orderType").toString().length()<0||pm.get("orderType").toString()==""){
            pm.put("orderType","一般内销定单");
        }
        List<Map<String,Object>> mapList = networkSupplyDao.select2(pm);
        JSONArray data=new JSONArray();
        JSONArray y1=new JSONArray();
        JSONArray y2=new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=0;i<mapList.size();i++){
            data.add(mapList.get(i).get("months"));
            y1.add(mapList.get(i).get("quantity"));
            y2.add(mapList.get(i).get("counts"));
        }
        ret.put("data1",data);
        ret.put("data2",y1);
        ret.put("data3",y2);
        return callbackSuccess(ret);
    }
}
